home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / tables / picmask.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-08-23  |  23.8 KB  |  639 lines

  1. VERSION 5.00
  2. Begin VB.Form frmPicMask 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Mask, transparency and raster operations"
  5.    ClientHeight    =   5595
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   6645
  9.    Icon            =   "picmask.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   373
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   443
  17.    Begin VB.CommandButton cmdClear 
  18.       Caption         =   "Clear"
  19.       BeginProperty Font 
  20.          Name            =   "MS Sans Serif"
  21.          Size            =   8.25
  22.          Charset         =   0
  23.          Weight          =   700
  24.          Underline       =   0   'False
  25.          Italic          =   0   'False
  26.          Strikethrough   =   0   'False
  27.       EndProperty
  28.       Height          =   375
  29.       Left            =   3300
  30.       TabIndex        =   19
  31.       ToolTipText     =   "Use only one API, via reverse masked foreground"
  32.       Top             =   4830
  33.       Width           =   1020
  34.    End
  35.    Begin VB.CommandButton cmdExit 
  36.       Caption         =   "Exit"
  37.       BeginProperty Font 
  38.          Name            =   "MS Sans Serif"
  39.          Size            =   8.25
  40.          Charset         =   0
  41.          Weight          =   700
  42.          Underline       =   0   'False
  43.          Italic          =   0   'False
  44.          Strikethrough   =   0   'False
  45.       EndProperty
  46.       Height          =   375
  47.       Left            =   4350
  48.       TabIndex        =   18
  49.       Top             =   4830
  50.       Width           =   1020
  51.    End
  52.    Begin VB.PictureBox picTransparent 
  53.       AutoRedraw      =   -1  'True
  54.       Height          =   1575
  55.       Left            =   4650
  56.       ScaleHeight     =   101
  57.       ScaleMode       =   3  'Pixel
  58.       ScaleWidth      =   102
  59.       TabIndex        =   17
  60.       Top             =   2910
  61.       Width           =   1590
  62.    End
  63.    Begin VB.PictureBox picFgd 
  64.       AutoRedraw      =   -1  'True
  65.       Height          =   1545
  66.       Left            =   2490
  67.       Picture         =   "picmask.frx":030A
  68.       ScaleHeight     =   99
  69.       ScaleMode       =   3  'Pixel
  70.       ScaleWidth      =   102
  71.       TabIndex        =   16
  72.       Top             =   630
  73.       Width           =   1590
  74.    End
  75.    Begin VB.CommandButton cmdMethod2 
  76.       Caption         =   "Method 2"
  77.       BeginProperty Font 
  78.          Name            =   "MS Sans Serif"
  79.          Size            =   8.25
  80.          Charset         =   0
  81.          Weight          =   700
  82.          Underline       =   0   'False
  83.          Italic          =   0   'False
  84.          Strikethrough   =   0   'False
  85.       EndProperty
  86.       Height          =   375
  87.       Left            =   2250
  88.       TabIndex        =   14
  89.       ToolTipText     =   "Involve many APIs and DCs, via transparent bitmap"
  90.       Top             =   4830
  91.       Width           =   1020
  92.    End
  93.    Begin VB.PictureBox picReverseMaskedFgd 
  94.       AutoRedraw      =   -1  'True
  95.       Height          =   1545
  96.       Left            =   4650
  97.       ScaleHeight     =   99
  98.       ScaleMode       =   3  'Pixel
  99.       ScaleWidth      =   104
  100.       TabIndex        =   5
  101.       Top             =   2910
  102.       Width           =   1620
  103.    End
  104.    Begin VB.CommandButton cmdMethod1 
  105.       Caption         =   "Method 1"
  106.       BeginProperty Font 
  107.          Name            =   "MS Sans Serif"
  108.          Size            =   8.25
  109.          Charset         =   0
  110.          Weight          =   700
  111.          Underline       =   0   'False
  112.          Italic          =   0   'False
  113.          Strikethrough   =   0   'False
  114.       EndProperty
  115.       Height          =   375
  116.       Left            =   1200
  117.       TabIndex        =   4
  118.       ToolTipText     =   "Use only one API, via reverse masked foreground"
  119.       Top             =   4830
  120.       Width           =   1020
  121.    End
  122.    Begin VB.PictureBox picProduct 
  123.       AutoRedraw      =   -1  'True
  124.       Height          =   1575
  125.       Left            =   4710
  126.       ScaleHeight     =   101
  127.       ScaleMode       =   3  'Pixel
  128.       ScaleWidth      =   102
  129.       TabIndex        =   3
  130.       Top             =   630
  131.       Width           =   1590
  132.    End
  133.    Begin VB.PictureBox picReversedMask 
  134.       AutoRedraw      =   -1  'True
  135.       Height          =   1515
  136.       Left            =   2520
  137.       ScaleHeight     =   97
  138.       ScaleMode       =   3  'Pixel
  139.       ScaleWidth      =   98
  140.       TabIndex        =   2
  141.       Top             =   2910
  142.       Width           =   1530
  143.    End
  144.    Begin VB.PictureBox picMask 
  145.       AutoRedraw      =   -1  'True
  146.       Height          =   1575
  147.       Left            =   270
  148.       ScaleHeight     =   101
  149.       ScaleMode       =   3  'Pixel
  150.       ScaleWidth      =   102
  151.       TabIndex        =   1
  152.       Top             =   2880
  153.       Width           =   1590
  154.    End
  155.    Begin VB.PictureBox picBackgd 
  156.       AutoRedraw      =   -1  'True
  157.       AutoSize        =   -1  'True
  158.       Height          =   1650
  159.       Left            =   270
  160.       Picture         =   "picmask.frx":7FFC
  161.       ScaleHeight     =   106
  162.       ScaleMode       =   3  'Pixel
  163.       ScaleWidth      =   102
  164.       TabIndex        =   0
  165.       Top             =   630
  166.       Width           =   1590
  167.    End
  168.    Begin VB.PictureBox picUnblockedFgd 
  169.       AutoRedraw      =   -1  'True
  170.       Height          =   1545
  171.       Left            =   2520
  172.       ScaleHeight     =   99
  173.       ScaleMode       =   3  'Pixel
  174.       ScaleWidth      =   99
  175.       TabIndex        =   15
  176.       Top             =   2910
  177.       Width           =   1545
  178.    End
  179.    Begin VB.Label lblEqual 
  180.       Caption         =   "="
  181.       BeginProperty Font 
  182.          Name            =   "MS Sans Serif"
  183.          Size            =   13.5
  184.          Charset         =   0
  185.          Weight          =   700
  186.          Underline       =   0   'False
  187.          Italic          =   0   'False
  188.          Strikethrough   =   0   'False
  189.       EndProperty
  190.       Height          =   315
  191.       Left            =   4290
  192.       TabIndex        =   13
  193.       Top             =   1170
  194.       Width           =   225
  195.    End
  196.    Begin VB.Label lblPlus 
  197.       Caption         =   "+"
  198.       BeginProperty Font 
  199.          Name            =   "MS Sans Serif"
  200.          Size            =   13.5
  201.          Charset         =   0
  202.          Weight          =   700
  203.          Underline       =   0   'False
  204.          Italic          =   0   'False
  205.          Strikethrough   =   0   'False
  206.       EndProperty
  207.       Height          =   315
  208.       Left            =   2040
  209.       TabIndex        =   12
  210.       Top             =   1200
  211.       Width           =   225
  212.    End
  213.    Begin VB.Label lblEndResult 
  214.       Alignment       =   2  'Center
  215.       Caption         =   "Product"
  216.       BeginProperty Font 
  217.          Name            =   "MS Sans Serif"
  218.          Size            =   8.25
  219.          Charset         =   0
  220.          Weight          =   700
  221.          Underline       =   0   'False
  222.          Italic          =   0   'False
  223.          Strikethrough   =   0   'False
  224.       EndProperty
  225.       Height          =   255
  226.       Left            =   4860
  227.       TabIndex        =   11
  228.       Top             =   240
  229.       Width           =   1380
  230.    End
  231.    Begin VB.Label lblReversedMask 
  232.       Alignment       =   2  'Center
  233.       Caption         =   "Reversed mask"
  234.       Height          =   375
  235.       Left            =   2670
  236.       TabIndex        =   10
  237.       Top             =   2460
  238.       Width           =   1230
  239.    End
  240.    Begin VB.Label lblReverseMaskedFgd 
  241.       Alignment       =   2  'Center
  242.       Caption         =   "Reverse-masked foreground"
  243.       Height          =   405
  244.       Left            =   4800
  245.       TabIndex        =   9
  246.       Top             =   2460
  247.       Width           =   1350
  248.    End
  249.    Begin VB.Label lblBackGround 
  250.       Alignment       =   2  'Center
  251.       Caption         =   "Background"
  252.       BeginProperty Font 
  253.          Name            =   "MS Sans Serif"
  254.          Size            =   8.25
  255.          Charset         =   0
  256.          Weight          =   700
  257.          Underline       =   0   'False
  258.          Italic          =   0   'False
  259.          Strikethrough   =   0   'False
  260.       EndProperty
  261.       Height          =   255
  262.       Left            =   480
  263.       TabIndex        =   8
  264.       Top             =   240
  265.       Width           =   1020
  266.    End
  267.    Begin VB.Label lblMask 
  268.       Alignment       =   2  'Center
  269.       Caption         =   "Mask"
  270.       Height          =   285
  271.       Left            =   510
  272.       TabIndex        =   7
  273.       Top             =   2550
  274.       Width           =   1050
  275.    End
  276.    Begin VB.Label lblForeGround 
  277.       Alignment       =   2  'Center
  278.       Caption         =   "Foreground"
  279.       BeginProperty Font 
  280.          Name            =   "MS Sans Serif"
  281.          Size            =   8.25
  282.          Charset         =   0
  283.          Weight          =   700
  284.          Underline       =   0   'False
  285.          Italic          =   0   'False
  286.          Strikethrough   =   0   'False
  287.       EndProperty
  288.       Height          =   255
  289.       Left            =   2790
  290.       TabIndex        =   6
  291.       Top             =   240
  292.       Width           =   1020
  293.    End
  294. Attribute VB_Name = "frmPicMask"
  295. Attribute VB_GlobalNameSpace = False
  296. Attribute VB_Creatable = False
  297. Attribute VB_PredeclaredId = True
  298. Attribute VB_Exposed = False
  299. ' PicMask.frm
  300. ' By Herman Liu
  301. ' If you have problems in grasping the basics of mask and raster operations, or
  302. ' find it difficult to digest the materials you have tried hard to obtain, it
  303. ' is not yourself to blame. Most of the materials around on these topics are either
  304. ' too "dry" or only "half-baked" for non-professionals, some are even obsolete.
  305. ' This code attempts to knit together the essential points pertaining to the
  306. ' inter-relationship of mask, transparency and raster operations, in a live example.
  307. ' Hope it will be of help to you.
  308. ' The exercise is to overlay a given picture onto another; the latter being the
  309. ' background of the new picture.  Two different approaches are presented; it is up
  310. ' to you to choose which one to follow (or both).
  311. Option Explicit
  312. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _
  313.     ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _
  314.     ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  315. '--------------------------------------------------------
  316. ' The following APIs are required only when Method 2 is opted
  317. '--------------------------------------------------------
  318. Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, _
  319.     ByVal crColor As Long) As Long
  320. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
  321.     ByVal y As Long) As Long
  322. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
  323.     ByVal nWidth As Long, ByVal nHeight As Long) As Long
  324. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  325. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
  326.     ByVal hObject As Long) As Long
  327. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  328. Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight _
  329.     As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
  330. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  331. Dim picW As Long
  332. Dim picH As Long
  333. Dim mresult
  334.             
  335. Private Sub Form_Load()
  336.     Me.Move 0, 0
  337.     picBackgd.AutoRedraw = True
  338.     picBackgd.AutoSize = True
  339.     picFgd.AutoRedraw = True
  340.     picFgd.AutoSize = False
  341.     picMask.AutoRedraw = True
  342.     picMask.AutoSize = False
  343.     picProduct.AutoRedraw = True
  344.     picProduct.AutoSize = True
  345.       ' For use if Method 1 only
  346.     picReverseMaskedFgd.AutoRedraw = True
  347.     picReverseMaskedFgd.AutoSize = False
  348.     picReversedMask.AutoRedraw = True
  349.     picReversedMask.AutoSize = False
  350.       ' For use if Method 2 only (Not shared, for clarity purposes)
  351.     picUnblockedFgd.AutoRedraw = True
  352.     picUnblockedFgd.AutoSize = False
  353.     picTransparent.AutoRedraw = True
  354.     picTransparent.AutoSize = False
  355.     picBackgd.Width = picFgd.Width
  356.     picBackgd.Height = picFgd.Height
  357.     picMask.Width = picFgd.Width
  358.     picMask.Height = picFgd.Height
  359.     picProduct.Width = picFgd.Width
  360.     picProduct.Height = picFgd.Height
  361.     picReverseMaskedFgd.Height = picFgd.Height
  362.     picReverseMaskedFgd.Width = picFgd.Width
  363.     picReversedMask.Height = picFgd.Height
  364.     picReversedMask.Width = picFgd.Width
  365.     picUnblockedFgd.Width = picFgd.Width
  366.     picUnblockedFgd.Height = picFgd.Height
  367.     picTransparent.Width = picFgd.Width
  368.     picTransparent.Height = picFgd.Height
  369.       ' Align
  370.     picFgd.Top = picBackgd.Top
  371.     picProduct.Top = picBackgd.Top
  372.     picUnblockedFgd.Top = picMask.Top
  373.     picReverseMaskedFgd.Top = picMask.Top
  374.     picTransparent.Top = picMask.Top
  375.     picReversedMask.Top = picMask.Top
  376.     picMask.Left = picBackgd.Left
  377.     picUnblockedFgd.Left = picFgd.Left
  378.     picReverseMaskedFgd.Left = picFgd.Left
  379.     picTransparent.Left = picProduct.Left
  380.     picReversedMask.Left = picProduct.Left
  381.       ' For convenience
  382.     picW = picBackgd.ScaleWidth
  383.     picH = picBackgd.ScaleHeight
  384.       ' Default these first. The following two labels
  385.       ' are shared by Method 1 and 2.
  386.     lblMask.Caption = ""
  387.     lblReverseMaskedFgd.Caption = ""
  388.     lblReversedMask.Caption = ""
  389.     picUnblockedFgd.Visible = False
  390.     picTransparent.Visible = False
  391. End Sub
  392. ' To blacken the non-white area
  393. Sub CreateImageMask(inPic As PictureBox, inColorToUse)
  394.     On Error Resume Next
  395.     Dim mTranspColor As Long
  396.     Dim i, j
  397.     mTranspColor = inPic.Point(1, 1)
  398.         ' See if existing background is fully covered by
  399.         ' some foreground color which is to serve as
  400.         ' background visually. We are to use image of
  401.         ' picBackgd as the background.
  402.     If mTranspColor <> inColorToUse Then
  403.         For j = 0 To picH + 1
  404.             For i = 0 To picW + 1
  405.                 If inPic.Point(j, i) = mTranspColor Then
  406.                     inPic.PSet (j, i), vbWhite
  407.                 End If
  408.             Next i
  409.             DoEvents
  410.         Next j
  411.     End If
  412.     For j = 0 To picH + 1
  413.         For i = 0 To picW + 1
  414.             If inPic.Point(j, i) <> vbWhite Then
  415.                 inPic.PSet (j, i), inColorToUse
  416.             End If
  417.         Next i
  418.         DoEvents
  419.     Next j
  420. End Sub
  421. Private Sub cmdMethod1_Click()
  422.     On Error Resume Next
  423.     Me.MousePointer = vbHourglass
  424.     picMask.Cls
  425.     picMask.Picture = LoadPicture()
  426.     picReverseMaskedFgd.Cls
  427.     picReverseMaskedFgd.Picture = LoadPicture()
  428.     picReversedMask.Cls
  429.     picReversedMask.Picture = LoadPicture()
  430.     picProduct.Cls
  431.     picProduct.Picture = LoadPicture()
  432.     lblMask.Caption = "Mask"
  433.     lblReverseMaskedFgd.Caption = "Reverse-masked foreground"
  434.     picReverseMaskedFgd.Visible = True
  435.     picTransparent.Visible = False
  436.     lblReversedMask.Caption = "Reversed mask"
  437.     picReversedMask.Visible = True
  438.     picUnblockedFgd.Visible = False
  439.         ' (For method 1, we will superimpose on picProduct the
  440.         ' reverse masked foreground, not the picFgd itself, hence
  441.         ' we don't have to call doUnBlockForeGround as we do in
  442.         ' the case of method 2)
  443.         
  444.         ' Prepare picMask (get a replica of foregound image, then mask it)
  445.     mresult = BitBlt(picMask.hdc, 0, 0, picW, picH, _
  446.          picFgd.hdc, 0, 0, vbSrcCopy)
  447.          
  448.      ' Do masking
  449.     CreateImageMask picMask, vbBlack
  450.     picMask.Picture = picMask.Image
  451.          ' Background picBackgd can readily be copied onto picProduct
  452.     BitBlt picProduct.hdc, 0, 0, picW, picH, picBackgd.hdc, 0, 0, vbSrcCopy
  453.     picProduct.Picture = picProduct.Image
  454.        ' Copy the mask onto the picProduct using the vbMergePaint opcode
  455.        ' to erase pixels corresponding to black parts of the mask.
  456.     BitBlt picProduct.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbMergePaint
  457.     picProduct.Picture = picProduct.Image
  458.     CreateReverseMaskedFgd
  459.        ' Copy the reverse masked Fgd image onto the masked background
  460.     BitBlt picProduct.hdc, 0, 0, picW, picH, picReverseMaskedFgd.hdc, _
  461.           0, 0, vbSrcAnd
  462.     picProduct.Picture = picProduct.Image
  463.     Me.MousePointer = vbDefault
  464. End Sub
  465. ' For creating reverse-masked foreground as an intermediary
  466. Private Sub CreateReverseMaskedFgd()
  467.        ' Make a reversed mask.
  468.     BitBlt picReversedMask.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbNotSrcCopy
  469.     picReversedMask.Picture = picReversedMask.Image
  470.        ' Copy picFgd to picReverseMaskedFgd
  471.     BitBlt picReverseMaskedFgd.hdc, 0, 0, picW, picH, picFgd.hdc, _
  472.         0, 0, vbSrcCopy
  473.     picReverseMaskedFgd.Picture = picReverseMaskedFgd.Image
  474.        ' Copy the earlier reversed mask onto the picRevserseMaskedFgd
  475.        ' using vbMergePaint opcode to erase part of the foreground
  476.        ' which corresponds to the black parts of that reversed mask.
  477.     BitBlt picReverseMaskedFgd.hdc, 0, 0, picW, picH, picReversedMask.hdc, _
  478.            0, 0, vbMergePaint
  479.     picReverseMaskedFgd.Picture = picReverseMaskedFgd.Image
  480. End Sub
  481. Private Sub cmdMethod2_Click()
  482.     On Error Resume Next
  483.     Me.MousePointer = vbHourglass
  484.     picMask.Cls
  485.     picMask.Picture = LoadPicture()
  486.     picUnblockedFgd.Cls
  487.     picUnblockedFgd.Picture = LoadPicture()
  488.     picTransparent.Cls
  489.     picTransparent.Picture = LoadPicture()
  490.     picProduct.Cls
  491.     picProduct.Picture = LoadPicture()
  492.     lblMask.Caption = "Mask"
  493.     lblReversedMask.Caption = "Unblocked foreground"
  494.     picUnblockedFgd.Visible = True
  495.     picReversedMask.Visible = False
  496.     lblReverseMaskedFgd.Caption = "Transparent bitmap"
  497.     picTransparent.Visible = True
  498.     picReverseMaskedFgd.Visible = False
  499.         ' For method 2, we have to check if entire foreground of picFgd
  500.         ' is painted; if so change it, but reflect the change in
  501.         ' picUnblockedFgd only (as after being made transparent it is
  502.         ' this one to be superimposed on picProduct)
  503.         '
  504.         ' Make a copy of picFgd for picUnblockedFgd first
  505.     mresult = BitBlt(picUnblockedFgd.hdc, 0, 0, picW, picH, _
  506.          picFgd.hdc, 0, 0, vbSrcCopy)
  507.     picUnblockedFgd.Picture = picUnblockedFgd.Image
  508.          
  509.         ' Unblock existing background as we are to use a
  510.         ' specified background as per picBackgd.
  511.     doUnBlockForeGround picFgd, picUnblockedFgd
  512.         ' Use the unblocked foreground to prepare picMask (get a
  513.         ' replica of foregound image, then mask it).
  514.       ' Make a copy of picUnblockedFgd for its masking
  515.     mresult = BitBlt(picMask.hdc, 0, 0, picW, picH, _
  516.          picUnblockedFgd.hdc, 0, 0, vbSrcCopy)
  517.       ' Do masking
  518.     CreateImageMask picMask, vbBlack
  519.     picMask.Picture = picMask.Image
  520.          ' Background picBackgd can readily be copied onto picProduct
  521.     BitBlt picProduct.hdc, 0, 0, picW, picH, picBackgd.hdc, _
  522.            0, 0, vbSrcCopy
  523.     picProduct.Picture = picProduct.Image
  524.        ' Copy the mask onto the picProduct using the vbMergePaint opcode
  525.        ' to erase pixels corresponding to black parts of the mask.
  526.     BitBlt picProduct.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbMergePaint
  527.     picProduct.Picture = picProduct.Image
  528.        ' Continue with other processes
  529.     Dim mColorAsTransparentr As Long
  530.        ' vbWhite as it is that part of area to become transparent in this case
  531.     mColorAsTransparentr = vbWhite
  532.     MakeTransparentPic picUnblockedFgd, picTransparent, _
  533.              mColorAsTransparentr
  534.              
  535.        ' Put the transparent picFgd on picProduct
  536.     BitBlt picProduct.hdc, 0, 0, picW, picH, picTransparent.hdc, _
  537.           0, 0, vbSrcAnd
  538.     picProduct.Picture = picProduct.Image
  539.     Me.MousePointer = vbDefault
  540.     DoEvents
  541. End Sub
  542. ' For creating a transparent bitmap as an intermediary
  543. Sub MakeTransparentPic(inpicSrc As PictureBox, inpicDest As PictureBox, _
  544.        inTransColor As Long)
  545.     On Error Resume Next
  546.     Dim mMaskDC As Long
  547.     Dim mMaskBmp As Long
  548.     Dim mTempMaskBMP As Long
  549.     Dim mMonoBMP As Long
  550.     Dim mMonoDC As Long
  551.     Dim mTempMonoBMP As Long
  552.     Dim mSrcHDC As Long, mDestHDC As Long
  553.     Dim w As Long, h As Long
  554.     w = inpicSrc.ScaleWidth
  555.     h = inpicSrc.ScaleHeight
  556.     mSrcHDC = inpicSrc.hdc
  557.     mDestHDC = inpicDest.hdc
  558.      ' Set back color of source pic and dest pic to
  559.      ' the desired transparent color
  560.     mresult = SetBkColor&(mSrcHDC, inTransColor)
  561.     mresult = SetBkColor&(mDestHDC, inTransColor)
  562.     ' Create a mask DC compatible with dest image
  563.     mMaskDC = CreateCompatibleDC(mDestHDC)
  564.     ' and a bitmap of its size
  565.     mMaskBmp = CreateCompatibleBitmap(mDestHDC, w, h)
  566.     ' Move that bitmap into mMaskDC
  567.     mTempMaskBMP = SelectObject(mMaskDC, mMaskBmp)
  568.     ' Meanwhile create another DC for mono bitmap by
  569.     '  setting nPlane and nbitCount both to 1.
  570.     mMonoDC = CreateCompatibleDC(mDestHDC)
  571.     '  and its bitmap, a mono one.
  572.     mMonoBMP = CreateBitmap(w, h, 1, 1, 0)
  573.     mTempMonoBMP = SelectObject(mMonoDC, mMonoBMP)
  574.     ' Copy source image to mMonoDC
  575.     mresult = BitBlt(mMonoDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcCopy)
  576.         
  577.     ' Copy the mMonoDC into mMaskDC
  578.     mresult = BitBlt(mMaskDC, 0, 0, w, h, mMonoDC, 0, 0, vbSrcCopy)
  579.     'We don't need mMonoBMP any longer
  580.     mMonoBMP = SelectObject(mMonoDC, mTempMonoBMP)
  581.     mresult = DeleteObject(mMonoBMP)
  582.     mresult = DeleteDC(mMonoDC)
  583.     'Now copy source image to dest image with XOR
  584.     mresult = BitBlt(mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert)
  585.     'Copy the mMaskDC to dest image with AND
  586.     mresult = BitBlt(mDestHDC, 0, 0, w, h, mMaskDC, 0, 0, vbSrcAnd)
  587.     'Copy source image to dest image with XOR
  588.     BitBlt mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert
  589.     'Picture is there to stay
  590.     inpicDest.Picture = inpicDest.Image
  591.      
  592.     ' We don't need mask DC and bitmap.
  593.     mMaskBmp = SelectObject(mMaskDC, mTempMaskBMP)
  594.     mresult = DeleteObject(mMaskBmp)
  595.     mresult = DeleteDC(mMaskDC)
  596. End Sub
  597. ' Called only if Method 2 is deployed.
  598. Private Sub doUnBlockForeGround(inPic1 As PictureBox, inPic2 As PictureBox)
  599.     Dim mTranspColor
  600.     Dim i, j
  601.     mTranspColor = inPic1.Point(1, 1)
  602.     If mTranspColor <> vbWhite Then
  603.         For j = 0 To picH + 1
  604.             For i = 0 To picW + 1
  605.                 If inPic1.Point(j, i) = mTranspColor Then
  606.                       ' We keep inPic1 as it is, but change inPic2
  607.                       ' we are to use inPic2 if Method 2 is deployed.
  608.                     inPic2.PSet (j, i), vbWhite
  609.                 End If
  610.             Next i
  611.         Next j
  612.         DoEvents                    ' See something going on
  613.     End If
  614. End Sub
  615. Private Sub cmdClear_Click()
  616.     picMask.Cls
  617.     picMask.Picture = LoadPicture()
  618.     picReverseMaskedFgd.Cls
  619.     picReverseMaskedFgd.Picture = LoadPicture()
  620.     picReversedMask.Cls
  621.     picReversedMask.Picture = LoadPicture()
  622.     picProduct.Cls
  623.     picProduct.Picture = LoadPicture()
  624.     picUnblockedFgd.Cls
  625.     picUnblockedFgd.Picture = LoadPicture()
  626.     picTransparent.Cls
  627.     picTransparent.Picture = LoadPicture()
  628.     lblMask.Caption = ""
  629.     lblReverseMaskedFgd.Caption = ""
  630.     picReverseMaskedFgd.Visible = True
  631.     picTransparent.Visible = False
  632.     lblReversedMask.Caption = ""
  633.     picReversedMask.Visible = True
  634.     picUnblockedFgd.Visible = False
  635. End Sub
  636. Private Sub cmdExit_Click()
  637.     Unload Me
  638. End Sub
  639.